//
//  LPTEarningsViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/6.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTEarningsViewController.h"
#import "LPTEarningHeadView.h"
#import "EarnTableViewCell.h"
#import "LPTIncomeDetailTableViewController.h"

@interface LPTEarningsViewController ()<UITableViewDelegate,UITableViewDataSource,LPTEarningHeadViewDelegate>

@property (nonatomic,strong)LPTEarningHeadView *headView;
@property (nonatomic,strong)NSIndexPath *indexPath;

@end

@implementation LPTEarningsViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self getIncomeList];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showMsgDetailVC"]) {
        
        LPTIncomeDetailTableViewController *vc = segue.destinationViewController;
        vc.type = kShouyimingxiType;
        vc.dic = self.dataSource[self.indexPath.row];
        
    }
}

-(void)initSubViews
{
    self.tableView.tableFooterView = [UIView new];
    LPTEarningHeadView *headView = [[LPTEarningHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 137)];
    headView.delegate = self;
    self.tableView.tableHeaderView = headView;
    self.headView = headView;
    self.tableView.rowHeight = 67;
    
    UserInfoModel *info = [[LTPCacheManager shareInstance] getUserInfoModel];
    [self.headView refreshWithDic:info];
}

-(void)getIncomeList
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/income_list" withBody:@{@"page":@(1),@"size":@(1000)}];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"incomes"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

#pragma mark - UITableViewDataSource & Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EarnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EarnTableViewCell" forIndexPath:indexPath];
    [cell refreshWithDic:self.dataSource[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.indexPath = indexPath;
    
    [self performSegueWithIdentifier:@"showMsgDetailVC" sender:nil];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"收益明细";
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

#pragma mark - LPTEarningHeadViewDelegate
-(void)earningHeadViewDidClickWithdrawal
{
    [self performSegueWithIdentifier:@"pushToWithDrawViewController" sender:nil];
}

@end
